<template>
  <div class="case">
    <na-table
      :datas.sync="tableData"
      :searchObj.sync="search"
      @updateDatas="getAllLists"
      @newAdd="addClick"
    >
    </na-table>
    <!--退货物料明细弹框    -->
    <el-dialog
      title="退货物料明细"
      :visible.sync="isReturnInfoDialog"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
    >
      <div class="dialogDody marDialog flex flex_column">
        <div class="btnCon flex flex_end">
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="infoSearch.likeWords"
              placeholder="输入关键字"
              size="small"
            >
              <el-button
                slot="append"
                v-loading="returnMarLoading"
                type="primary"
                icon="el-icon-search"
                :disabled="returnMarLoading"
                element-loading-spinner="el-icon-loading"
                @click="getReturnInfo"
              >
                搜索
              </el-button>
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon">
          <el-table
            v-loading="returnMarLoading"
            :data="returnGoodsInfo"
            border
            style="width: 100%"
          >
            <el-table-column type="index" label="序号" width="55" />
            <el-table-column
              prop="stockGoodsCode"
              label="物料编号"
              min-width="110"
            />
            <el-table-column
              prop="stockGoodsName"
              label="物料名称"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column
              prop="specificationName"
              label="规格型号"
              min-width="100"
            />
            <el-table-column
              prop="purchaseOrderNo"
              label="批次编码"
              min-width="100"
            />
            <el-table-column prop="groupName" label="物料分组" min-width="90" />
            <el-table-column
              prop="categoryName"
              label="物料类型"
              min-width="90"
            />
            <el-table-column
              prop="supplierName"
              label="供应商"
              min-width="90"
            />
            <el-table-column prop="unitName" label="物料单位" min-width="100" />
            <el-table-column
              prop="specificationJsValue"
              label="m²/每米"
              min-width="90"
            />
            <el-table-column
              prop="creditQuantity"
              label="退货数量"
              min-width="90"
            />
            <el-table-column
              prop="creditSquare"
              label="退货平方米"
              min-width="110"
            />
            <el-table-column
              prop="creditPrice"
              label="退货单价"
              min-width="90"
            />
            <el-table-column
              prop="creditValue"
              label="退货总金额"
              min-width="100"
            />
            <el-table-column
              prop="warehouseName"
              label="退货仓库"
              min-width="90"
            />
            <el-table-column
              prop="areaShelfName"
              label="退货库位"
              min-width="90"
            />
            <el-table-column
              prop="creditBillNo"
              label="退货单号"
              min-width="90"
            />
            <el-table-column
              prop="purchaseOrderNo"
              label="采购单号"
              min-width="90"
            />
            <el-table-column
              prop="creditDetailRemark"
              label="备注"
              min-width="150"
              show-overflow-tooltip
            />
          </el-table>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <!--    分页-->
        <div class="paginaCon noborder">
          <pagination
            :total="returnToal || 0"
            :page.sync="infoSearch.pageIndex"
            :limit.sync="infoSearch.pageSize"
            @pagination="getReturnInfo"
          />
        </div>
      </div>
    </el-dialog>
    <!-- 新建退货 -->
    <el-dialog
      :title="'退货单-' + modelTitle"
      :visible.sync="isAddDialog"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"
            ><span class="mr5">退货单号：</span
            >{{ returnForm.creditBillNo || "系统自动生成" }}</span
          >
          <span class="mr15"
            ><span class="mr5">创建日期：</span
            >{{ returnForm.createTime || dayjs().format("YYYY-MM-DD") }}</span
          >
          <span
            ><span class="mr5">创建人员：</span
            >{{ ops === "modify" ? returnForm.createPersonName : name }}</span
          >
        </div>
        <div class="addWuliaoBox">
          <el-form
            ref="returnForm"
            :model="returnForm"
            :rules="rules"
            label-width="95px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="退货类型" prop="creditType">
                  <el-select
                        filterable
                    v-model="returnForm.creditType"
                    placeholder="请选择入库仓库"
                    size="mini"
                  >
                    <el-option
                      :label="item.name"
                      :value="item.code"
                      v-for="item in creditTypeList"
                      :key="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="关联单据" prop="purchaseOrderNo">
                  <el-input
                    v-model="returnForm.purchaseOrderNo"
                    placeholder="关联单据"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="退货人员">
                  <el-input
                    :value="returnForm.creditPersonName || name"
                    placeholder="请输入内容"
                    size="mini"
                  >
                    <template slot="append">
                      <el-button
                        slot="append"
                        icon="el-icon-user"
                        @click="isperShow = true"
                      />
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierName">
                  <el-input
                    v-model="returnForm.supplierName"
                    placeholder="供应商"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item
                  label="退货备注"
                  class="inlineText"
                  prop="creditBillRemark"
                >
                  <el-input
                    v-model="returnForm.creditBillRemark"
                    type="textarea"
                    :rows="1"
                    placeholder="请输入退货备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">退货明细</span>
              <!-- <span class="c_3F61F1 pointer el-icon-refresh-right mr15" @click="newReturnRefresh"> 重置</span> -->
            </div>
            <el-table
              v-loading="purchaseLoading"
              :data="purchaseDetailList"
              border
              style="width: 100%"
            >
              <el-table-column
                label="物料编号"
                prop="specificationCode"
                show-overflow-tooltip
                min-width="110"
              />
              <el-table-column
                label="物料类型"
                prop="categoryName"
                min-width="90"
              />
              <el-table-column
                label="物料名称"
                prop="goodsName"
                show-overflow-tooltip
                min-width="110"
              />
              <el-table-column
                label="规格型号"
                prop="stockGoodsName"
                min-width="90"
              />
              <el-table-column
                label="生产批次"
                prop="productionBatchNumber"
                min-width="90"
              />
              <el-table-column
                label="生产日期"
                prop="productionDate"
                min-width="150"
              />
              <el-table-column
                label="采购数量"
                show-overflow-tooltip
                min-width="90"
                prop="purchaseQuantity"
              />
              <el-table-column
                label="库存数量"
                show-overflow-tooltip
                min-width="90"
                prop="stockQuantity"
              />
              <el-table-column
                label="已退数量"
                show-overflow-tooltip
                min-width="120"
                prop="creditedQuantity"
              />
              <el-table-column
                label="退货数量"
                show-overflow-tooltip
                min-width="120"
              >
                <template slot-scope="{ row }">
                  <el-input
                    v-model="row.creditQuantity"
                    @blur="changeCreditQuantity(row)"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="使用单位"
                prop="goodsUnitName"
                min-width="110"
              />
              <el-table-column
                label="转换比例"
                prop="specificationJsValue"
                min-width="110"
              />
              <el-table-column
                prop="priceUnitName"
                width="150"
                align="center"
                label="计价单位"
              >
              </el-table-column>
              <el-table-column
                label="单价"
                prop="creditPrice"
                min-width="110"
              />
              <el-table-column
                prop="creditValue"
                width="150"
                align="center"
                label="小计"
              >
              </el-table-column>
              <el-table-column
                label="备注"
                show-overflow-tooltip
                min-width="150"
              >
                <template slot-scope="{ row }">
                  <el-input
                    v-model="row.creditDetailRemark"
                    placeholder="请输入"
                    size="mini"
                  />
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                fixed="right"
                show-overflow-tooltip
                min-width="80"
              >
                <template slot-scope="scope">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="right-start"
                  >
                    <span
                      class="pointer"
                      @click="returnDelete(scope.$index, scope.row)"
                      ><i class="el-icon-delete"
                    /></span>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddDialog = false">取 消</el-button>
        <!-- 新增       -->
        <el-button
          v-if="ops === 'add'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveAdd"
          >保存</el-button
        >
        <el-button
          v-if="ops === 'add'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveAdd('confirm')"
          >保存并确认</el-button
        >
        <!--  修改      -->
        <el-button
          v-if="ops === 'modify'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="editSave"
          >保存</el-button
        >
        <el-button
          v-if="ops === 'modify'"
          v-loading="addLoading"
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="editSave('confirm')"
          >保存并确认</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看退货详情 -->
    <el-dialog
      :title="'退货单-' + modelTitle"
      :visible.sync="isSeeDialog"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      @close="closeDialog"
      width="85%"
    >
      <div class="dialogDody">
        <div class="subTip">
          <span class="mr15"
            ><span class="mr5">退货单号：</span
            >{{ returnForm.creditBillNo || "系统自动生成" }}</span
          >
          <span class="mr15"
            ><span class="mr5">创建日期：</span
            >{{ returnForm.createTime || dayjs().format("YYYY-MM-DD") }}</span
          >
          <span
            ><span class="mr5">创建人员：</span
            >{{ ops === "see" ? returnForm.createPersonName : name }}</span
          >
        </div>
        <div class="addWuliaoBox">
          <el-form
            :model="returnForm"
            disabled
            label-width="95px"
            label-position="left"
            class="dialogForm dialogInlineForm"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="退货类型" prop="creditType">
                  <el-select
                        filterable
                    v-model="returnForm.creditType"
                    placeholder="请选择入库仓库"
                    size="mini"
                  >
                    <el-option
                      :label="item.name"
                      :value="item.code"
                      v-for="item in creditTypeList"
                      :key="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="关联单据" prop="purchaseOrderNo">
                  <el-input
                    v-model="returnForm.purchaseOrderNo"
                    placeholder="关联单据"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="退货人员">
                  <el-input
                    v-model="returnForm.creditPersonName"
                    placeholder="请输入内容"
                    size="mini"
                  >
                    <template slot="append">
                      <el-button
                        slot="append"
                        icon="el-icon-user"
                        @click="isperShow = true"
                      />
                    </template>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="供应商" prop="supplierName">
                  <el-input
                    v-model="returnForm.supplierName"
                    placeholder="供应商"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item
                  label="退货备注"
                  class="inlineText"
                  prop="creditBillRemark"
                >
                  <el-input
                    v-model="returnForm.creditBillRemark"
                    type="textarea"
                    :rows="1"
                    placeholder="请输入退货备注"
                    size="mini"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">退货明细</span>
            </div>
            <el-table
              v-loading="purchaseLoading"
              :data="purchaseDetailList"
              border
              style="width: 100%"
            >
              <el-table-column
                label="物料编号"
                prop="specificationCode"
                show-overflow-tooltip
                min-width="110"
              />
              <el-table-column
                label="物料类型"
                prop="categoryName"
                min-width="90"
              />
              <el-table-column
                label="物料名称"
                prop="goodsName"
                show-overflow-tooltip
                min-width="110"
              />
              <el-table-column
                label="规格型号"
                prop="stockGoodsName"
                min-width="90"
              />
              <el-table-column
                label="生产批次"
                prop="productionBatchNumber"
                min-width="90"
              />
              <el-table-column
                label="生产日期"
                prop="productionDate"
                min-width="150"
              />
              <el-table-column
                label="采购数量"
                show-overflow-tooltip
                min-width="90"
                prop="purchaseQuantity"
              />
              <el-table-column
                label="已退数量"
                show-overflow-tooltip
                min-width="120"
                prop="creditedQuantity"
              />
              <el-table-column
                label="退货数量"
                show-overflow-tooltip
                min-width="120"
                prop="creditQuantity"
              />
              <el-table-column
                label="物料单位"
                prop="goodsUnitName"
                min-width="110"
              />
              <el-table-column
                label="转换比例"
                prop="specificationJsValue"
                min-width="110"
              />
              <el-table-column
                prop="priceUnitName"
                width="150"
                align="center"
                label="计价单位"
              >
              </el-table-column>
              <el-table-column
                prop="creditValue"
                width="150"
                align="center"
                label="小计"
              >
              </el-table-column>
              <el-table-column
                label="单价"
                prop="creditPrice"
                min-width="110"
              />
              <el-table-column
                label="备注"
                show-overflow-tooltip
                min-width="150"
                prop="creditDetailRemark"
              />
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isSeeDialog = false">取 消</el-button>
        <!--  确定      -->
        <el-button
          v-if="ops === 'see'"
          type="primary"
          size="mini"
          @click="isSeeDialog = false"
          >确定</el-button
        >
        <!--  确认退货      -->
        <el-button
          v-if="ops === 'confirm'"
          v-loading="confirmLoading"
          :disabled="confirmLoading"
          type="primary"
          size="mini"
          @click="confirmReturn"
          >确认退货</el-button
        >
        <el-button
          v-if="ops === 'examine'"
          v-loading="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="success"
          size="mini"
          @click="arrivalConfirm"
          >通过</el-button
        >
        <el-button
          v-if="ops === 'examine'"
          v-loading="addPurLoading"
          element-loading-spinner="el-icon-loading"
          type="warning"
          size="mini"
          @click="handleRefuse"
          >拒绝</el-button
        >
      </div>
    </el-dialog>
    <!-- 采购单明细 -->
    <el-dialog
      title="采购单明细"
      :visible.sync="isPurInfo"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
    >
      <div class="dialogDody">
        <!-- 搜索     -->
        <div class="btnCon flex flex_between" style="border-bottom: none">
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="purSearch.likeWords"
              prefix-icon="el-icon-search"
              placeholder="输入关键字"
              size="small"
              @keyup.enter.native="getPurInfo"
            >
              <!-- <el-button
                slot="append"
                v-loading="purLoading"
                type="primary"
                icon="el-icon-search"
                :disabled="purLoading"
                element-loading-spinner="el-icon-loading"
                @click="getPurInfo"
              >
                搜索
              </el-button> -->
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon pt0" style="padding: 16px">
          <el-table
            v-loading="purLoading"
            :data="purInfoLists"
            height="500"
            :row-class-name="tableRowClassName"
            border
            style="width: 100%"
          >
            <el-table-column
              prop="purchaseOrderNo"
              label="采购单号"
              min-width="110"
            />
            <el-table-column
              prop="purchaseTypeName"
              label="采购类型"
              min-width="100"
            />
            <el-table-column
              prop="createTime"
              label="采购时间"
              show-overflow-tooltip
              min-width="110"
            />
            <el-table-column prop="detailCount" label="明细" min-width="100" />
            <el-table-column prop="totalCount" label="总数量" min-width="90" />
            <el-table-column
              prop="totalAmount"
              label="采购总金额"
              show-overflow-tooltip
              width="120"
            />
            <el-table-column
              prop="expectArrivalTime"
              label="期望到货日期"
              show-overflow-tooltip
              min-width="120"
            />
            <el-table-column
              prop="supplierName"
              label="供应商"
              sortable
              min-width="120"
              show-overflow-tooltip
            />
            <el-table-column
              prop="purchasePersonName"
              label="申请人员"
              min-width="90"
            />
            <el-table-column
              prop="purchaseDesc"
              label="备注"
              min-width="150"
              show-overflow-tooltip
            >
              <template scope="{row}">
                {{ row.purchaseDesc || "--" }}
              </template>
            </el-table-column>

            <el-table-column
              label="操作"
              width="100px"
              fixed="right"
              align="center"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="medium"
                  style="color: #606266"
                  @click="confirmSelecd(scope.row)"
                >
                  选择
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="detailTotal || 0"
            :page.sync="purSearch.pageIndex"
            :limit.sync="purSearch.pageSize"
            @pagination="getPurInfo"
          />
        </div>
      </div>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isPurInfo=false">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="confirmSelecd"
        >确定</el-button>
      </span> -->
    </el-dialog>
    <!-- 人员寻找弹框   -->
    <!-- <per-redio-dialog
      :per-visible.sync="isperShow"
      @getEmployee="getEmployee"
    /> -->

    <perCheckDialog :per-visible.sync="isperShow" @getEmployee="getEmployee" />
    <!-- 作废原因弹框   -->
    <el-dialog
      :title="title + '原因'"
      :visible.sync="reasonVisible"
      :close-on-click-modal="false"
      custom-class="comDialog"
      append-to-body
      width="30%"
      @close="closeCancelDialog"
    >
      <div class="dialogDody">
        <el-form
          ref="cancelForm"
          :model="stateForm"
          :rules="cancelRules"
          label-width="0px"
          class="dialogForm"
        >
          <el-form-item label="" prop="reason">
            <el-input
              v-model="stateForm.reason"
              type="textarea"
              :autosize="{ minRows: 4 }"
              placeholder="请输入内容"
              size="mini"
              clearable
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="reasonVisible = false">取 消</el-button>
        <el-button
          v-loading="addLableLoading"
          :disabled="addLableLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="saveReason"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import naTable from "@/components/naTable";
import Pagination from "@/components/Pagination";
import perRedioDialog from "@/components/perDialogRedio";
import perCheckDialog from '@/components/perDialogCheckbox'
import { arraySort } from "@/utils";
import { mapGetters } from "vuex";
export default {
  name: "ReturnGoods",
  components: {
    naTable,
    perRedioDialog,
    Pagination,
    perCheckDialog
  },
  computed: {
    ...mapGetters(["name", "userInfo"]),
    modelTitle: function () {
      let title = "";
      if (this.ops == "add") {
        title = "新增";
      } else if (this.ops == "modify") {
        title = "编辑";
      } else if (this.ops == "see") {
        title = "查看";
      } else if (this.ops == "confirm") {
        title = "确认";
      } else if (this.ops == "examine") {
        title = "审核";
      }
      return title;
    },
  },
  data() {
    return {
      reasonVisible: false, // 作废弹框
      isperShow: false, // 人员选择弹框
      addLableLoading: false,
      radioSelect: null, // 采购单明细选中值 采购单号
      addPurLoading: false,
      purchaseLoading: false,
      detailTotal: 0,
      isSeeDialog: false, // 查看退货弹框是否打开
      isAddDialog: false, // 新建退货弹框是否打开
      confirmLoading: false, // 确认退货按钮加载效果
      ops: "add",
      isPurInfo: false, // 采购明细弹框是否显示
      addLoading: false, // 新建退货加载效果
      isReturnInfoDialog: false, // 退货明细弹框是否打开
      returnMarLoading: false,
      searchLoading: false, // 退货列表加载效果
      tableHeight: null,
      tableHead: [], // 表头
      purInfoLists: [], // 采购明细列表
      cancelForm: {
        remarks: "",
      }, // 作废表单
      creditTypeList: [], // 退货类型列表
      returnForm: {
        creditBillNo: "", // 退货单号
        creditType: "", // 退货类型
        supplierCode: "", // 供应商code
        supplierName: "", // 供应商名称
        creditPersonName: "", // 退货人员名称
        creditPeopleCode: "", // 退货人员code
        invoiceNumber: "", // 发票号
        purchaseOrderNo: "", // 关联采购单号
        creditBillRemark: "",
        detailList: [],
      }, // 新建退货表单
      purchaseDetailList: [], // 采购明细列表
      search: {
        QueryDataBaseFiled: "",
        likeWords: "",
        pageIndex: 1,
        pageSize: 30,
        supplierCode: "",
        creditState: -1,
      },
      dataLists: [], // 退货分页列表
      total: 0,
      infoSearch: {
        likeWords: "",
        pageIndex: 1,
        pageSize: 30,
        creditBillNo: "",
        stockGoodsCode: "",
        warehouseCode: "",
      },
      returnGoodsInfo: [], // 退货物料明细列表
      returnToal: 0,
      purLoading: false,
      purSearch: {
        purchaseOrderState: 2,
        stockState: 1,
        likeWords: "",
        pageIndex: 1,
        pageSize: 30,
      }, // 采购单明细搜索条件
      marTotale: 0,
      currentRow: [], // 采购明细选中当前项
      creditID: null,
      rules: {
        purchaseOrderNo: [
          {
            required: true,
            message: "请选择关联采购单",
            trigger: "change",
          },
        ],
        supplierName: [
          {
            required: true,
            message: "请选择供应商",
            trigger: "change",
          },
        ],
        creditType: [
          {
            required: true,
            message: "请选择退货类型",
            trigger: "change",
          },
        ],
        creditBillRemark: [
          {
            required: true,
            message: "请填写退货备注",
            trigger: "change",
          },
        ],
      },
      cancelRules: {
        reason: [
          {
            required: true,
            message: "请填写原因",
            trigger: "blur",
          },
        ],
      },
      title: "",
      stateForm: {
        creditBillNo: "",
        state: 0,
        reason: "",
      },

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "creditID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        // addTitle: '', //新建按钮名称-为''则不显示，为undefine则为新建
        addPermission: "MenuCode5-6-1", //新建按钮权限
        addIcon: "", //新建按钮icon
        voidWord: "", //需要查询作废的字段
        statusList: [], //状态筛选条件列表
        statusWord: "", //状态筛选的字段
        refresh: true, //刷新按钮
        showSum: true, //显示合计
        //表格按钮
        operationBtn: [
          {
            title: "查看", //按钮名称
            methodsName: "seeInfo", //按钮绑定方法的字段
          },
          {
            title: "修改", //按钮名称
            methodsName: "updateReturnGoods", //按钮绑定方法的字段
            permission: "MenuCode5-6-2", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "creditStateName", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "确认", //按钮名称
            methodsName: "confirmClick", //按钮绑定方法的字段
            permission: "MenuCode5-6-3", //按钮权限
            hideCondition: {
              //按钮显隐条件
              prop: "creditStateName", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "审核", //按钮名称
            methodsName: "handleArrivalClick", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "creditStateName", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待审核",
              ],
            },
          },
          {
            title: "作废", //按钮名称
            methodsName: "nullifyClick", //按钮绑定方法的字段
            hideCondition: {
              //按钮显隐条件
              prop: "creditStateName", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用-值必须跟状态的类型保持一致
                "待确认",
              ],
            },
          },
          {
            title: "打印退货单",
            methodsName: "handleDownload",
          },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },
    };
  },
  created() {
    // 退货列表
    this.getAllLists();
    // 退货表头
    this.getTableHead();
    // 退货物料明细
    // this.getReturnInfo()
    // 采购单明细
    this.getPurInfo();
    // 得到退货类型列表
    this.getCreditTypeList();
  },
  mounted() {},
  methods: {
    // 作废点击事件
    nullifyClick(data) {
      this.reasonVisible = true;
      this.title = "作废";
      this.stateForm.creditBillNo = data.creditBillNo;
      this.stateForm.state = 4;
    },
    // 保存原因
    saveReason() {
      this.$refs.cancelForm.validate((valid) => {
        if (valid) {
          this.saveState();
        }
      });
    },
    // 人员选择确定事件
    getEmployee(val) {
      this.returnForm.creditPeopleCode = val.employeeCode;
      this.returnForm.creditPersonName = val.employeeName;
    },
    // 得到退货类型列表
    getCreditTypeList() {
      this.$api.warehouse.GetAllCreditType().then((res) => {
        this.creditTypeList = res;
      });
    },

    //下载退货单
    handleDownload(row) {
      let parmas = {
        templateTypeCode: "MBLX0015", //	模板类型编号
        condition: {
          CreditBillNo: row.creditBillNo,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    // 技术采购总价
    getPurTotalPrice(data) {
      const price = Number(data.goodsPrice) || 0;
      const num = Number(data.planPurchaseQuantity) || 0;
      data.subtotal = num * price * Number(data.specificationJsValue);
      this.$forceUpdate();
    },
    // 删除退货单
    deleteReturn() {
      const parmas = {
        entityID: this.creditID,
      };
      this.$confirm("确定删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.warehouse.DeleteStockGoodsCredit(parmas).then(() => {
          this.getAllLists();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        });
      });
    },

    //审核通过
    arrivalConfirm() {
      this.stateForm.creditBillNo = this.returnForm.creditBillNo;
      this.stateForm.state = 2;
      this.saveState();
    },

    //审核拒绝
    handleRefuse() {
      this.stateForm.creditBillNo = this.returnForm.creditBillNo;
      this.stateForm.state = 3;
      this.title = "拒绝";
      this.reasonVisible = true;
    },

    saveState() {
      this.addPurLoading = true;
      this.$api.warehouse
        .UpdateStockGoodsCreditState(this.stateForm)
        .then((res) => {
          this.$message.success("保存成功");
          this.getAllLists();
          this.isSeeDialog = false;
          this.reasonVisible = false;
          this.addPurLoading = false;
        })
        .catch(() => {
          this.addPurLoading = false;
        });
    },

    // 确认退货
    confirmReturn() {
      this.stateForm.creditBillNo = this.returnForm.creditBillNo;
      this.stateForm.state = 1;
      this.saveState();
    },
    // 修改保存事件
    editSave(type) {
      this.$refs.returnForm.validate((valid) => {
        if (valid) {
          this.addLoading = true;
          this.returnForm.detailList = this.purchaseDetailList;
          const parmas = this.returnForm;
          if (type === "confirm") {
            this.$api.warehouse
              .UpdateStockGoodsCredit(parmas)
              .then(() => {
                let obj = {
                  creditBillNo: this.returnForm.creditBillNo,
                  state: 1,
                };
                this.$api.warehouse
                  .UpdateStockGoodsCreditState(obj)
                  .then((res) => {
                    this.$message.success("确认成功");
                    this.isAddDialog = false;
                    this.addLoading = false;
                    this.getAllLists();
                  })
                  .catch(() => {
                    this.addPurLoading = false;
                  });
                // this.isAddDialog = false;
                // this.addLoading = false;
                // // 退货仓库列表
                // this.getAllLists();
                // this.$message({
                //   message: "修改成功",
                //   type: "success",
                // });
              })
              .catch(() => {
                this.addLoading = false;
              });
          } else {
            this.$api.warehouse
              .UpdateStockGoodsCredit(parmas)
              .then(() => {
                this.isAddDialog = false;
                this.addLoading = false;
                // 退货仓库列表
                this.getAllLists();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addLoading = false;
              });
          }
        }
      });
    },
    // 获取详情
    getReturnGoodsInfo() {
      const parmas = {
        queryBy: 1,
        queryValue: this.creditID,
      };
      this.$api.warehouse.GetSingleStockGoodsCredit(parmas).then((res) => {
        this.returnForm = res;
        this.purchaseDetailList = [...res.detailList];
        this.billState = res.creditState;
      });
    },
    // 新建保存
    saveAdd(type) {
      this.$refs.returnForm.validate((valid) => {
        if (valid) {
          this.addLoading = true;
          this.returnForm.detailList = this.purchaseDetailList;
          const parmas = this.returnForm;
          if (type === "confirm") {
            this.$api.warehouse
              .AddStockGoodsCredit(parmas)
              .then((res) => {
                let obj = {
                  creditBillNo: res.creditBillNo,
                  state: 1,
                };
                this.$api.warehouse
                  .UpdateStockGoodsCreditState(obj)
                  .then((res) => {
                    this.$message.success("保存成功");
                    this.isAddDialog = false;
                    this.addLoading = false;
                    this.getAllLists();
                  })
                  .catch(() => {
                    this.addPurLoading = false;
                  });

                // 退货仓库列表
                this.getAllLists();
                // this.$message({
                //   message: "退货成功",
                //   type: "success",
                // });
              })
              .catch(() => {
                this.addLoading = false;
              });
          } else {
            this.$api.warehouse
              .AddStockGoodsCredit(parmas)
              .then(() => {
                this.isAddDialog = false;
                this.addLoading = false;
                // 退货仓库列表
                this.getAllLists();
                this.$message({
                  message: "退货成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addLoading = false;
              });
          }
        }
      });
    },
    newReturnRefresh() {
      this.purchaseDetailList = [...this.returnForm.detailList];
    },
    returnDelete(index, data) {
      const params = {
        entityID: data.detailID,
      };
      if (data.detailID) {
        this.$api.warehouse.DeleteStockGoodsCreditDetail(params).then((res) => {
          if (res.isDelete) {
            this.purchaseDetailList.splice(index, 1);
          } else {
            this.$message({
              message: "这条信息不能删除",
              type: "warning",
            });
          }
        });
      } else {
        this.purchaseDetailList.splice(index, 1);
      }
    },
    // 退货数量改变事件
    changeCreditQuantity(row) {
      row.creditValue =
        row.creditQuantity * row.creditPrice * row.specificationJsValue;
        row.creditValue = JSON.parse(row.creditValue.toFixed(4))
    },
    // 采购单明细选择确定
    confirmSelecd(row) {
      const arr = JSON.parse(JSON.stringify(row.purchaseDetailsList));
      arr.forEach((item) => {
        item.detailID = "";
        item.creditQuantity = 1;
        item.creditPrice = item.purchasePrice;
        item.creditDetailRemark = "";
        item.creditValue =  1 * item.purchasePrice;
        item.areaShelfCode = item.rkAreaShelfCode;
        item.areaShelfName = item.rkAreaShelfName;
        item.warehouseAreaCode = item.rkWarehouseAreaCode;
        item.warehouseAreaName = item.rkWarehouseAreaName;
        item.warehouseCode = item.rkWarehouseCode;
        item.warehouseName = item.rkWarehouseName;
        item.purchaseQuantity = item.planPurchaseQuantity;
        item.areaShelfName = item.rkAreaShelfName;
      });
      this.purchaseDetailList = [...arr];
      this.returnForm.detailList = [...arr];
      this.returnForm.purchaseOrderNo = row.purchaseOrderNo;
      this.returnForm.supplierCode = row.supplierCode;
      this.returnForm.supplierName = row.supplierName;
      this.returnForm.invoiceNumber = row.invoiceNumber;
      this.isPurInfo = false;
      this.isAddDialog = true;
    },
    // 新建退货按钮点击事件
    addClick() {
      this.ops = "add";
      this.isPurInfo = true;
      this.getPurInfo(); // 采购单列表
      this.returnForm.creditPeopleCode = this.userInfo.employeeCode;
      this.returnForm.creditPeopleName = this.userInfo.employeeName;
    },
    // 给选中一行添加class
    tableRowClassName({ row }) {
      if (this.currentRow.purchaseOrderNo === row.purchaseOrderNo) {
        return "selectd";
      }
    },
    // 采购单明细
    getPurInfo() {
      const parmas = this.purSearch;
      this.purLoading = true;
      this.$api.warehouse
        .getRuKuPurInfo(parmas)
        .then((res) => {
          this.purInfoLists = res.list;
          this.detailTotal = res.totalCount;
          this.purLoading = false;
        })
        .catch(() => {
          this.purLoading = false;
        });
    },
    // 退货物料明细
    getReturnInfo() {
      const parmas = this.infoSearch;
      this.returnMarLoading = true;
      this.$api.warehouse
        .GetMatchPageStockGoodsCreditDetails(parmas)
        .then((res) => {
          this.returnGoodsInfo = res.list;
          this.returnToal = res.totalCount;
          this.returnMarLoading = false;
        })
        .catch(() => {
          this.returnMarLoading = false;
        });
    },
    // 查看退货详情
    seeInfo(data) {
      // 查看
      this.ops = "see";
      this.creditID = data.creditID;
      this.getReturnGoodsInfo();
      this.isSeeDialog = true;
    },
    // 修改点击事件
    updateReturnGoods(data) {
      this.ops = "modify";
      this.creditID = data.creditID;
      this.isAddDialog = true;
      // 获取详情
      this.getReturnGoodsInfo();
    },
    // 确认点击事件
    confirmClick(data) {
      this.ops = "confirm";
      this.isSeeDialog = true;
      this.creditID = data.creditID;
      this.getReturnGoodsInfo();
    },
    //审核
    handleArrivalClick(data) {
      this.ops = "examine";
      this.isSeeDialog = true;
      this.creditID = data.creditID;
      this.getReturnGoodsInfo();
    },
    // 删除点击事件
    dleteClick(data) {
      this.creditID = data.creditID;
      this.deleteReturn();
    },
    // 退货仓库列表
    getAllLists() {
      const parmas = this.search;
      this.$api.warehouse.GetMatchPageStockGoodsCredits(parmas).then((res) => {
        if (res.list.length > 0) {
          res.list.forEach((item) => {
            if (item.purchaseOrderState == 0) {
              item.purchaseOrderState = "待确认";
            } else if (item.purchaseOrderState == 1) {
              item.purchaseOrderState = "待审核";
            } else if (item.purchaseOrderState == 2) {
              item.purchaseOrderState = "已完成";
            } else if (item.purchaseOrderState == 3) {
              item.purchaseOrderState = "已拒绝";
            } else if (item.purchaseOrderState == 4) {
              item.purchaseOrderState = "已作废";
            }
          });
        }
        this.tableData.tbody = res.list;
        this.search.total = res.totalCount;
      });
    },
    // 获取退货表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.stockGoodsCreditList,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
      });
    },
    // 合计方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (column.property === "creditSumValue") {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index]++;
          } else {
            sums[index] = " ";
          }
        }
      });

      return sums;
    },
    // 关闭弹框事件
    closeDialog() {
      if (this.$refs.returnForm) {
        this.$refs.returnForm.resetFields();
      }
      this.returnForm = this.$options.data().returnForm;
      this.purchaseDetailList = this.$options.data().purchaseDetailList;
    },
    closeCancelDialog() {
      if (this.$refs.cancelForm) {
        this.$refs.cancelForm.resetFields();
      }
      this.cancelForm = this.$options.data().cancelForm;
    },
  },
};
</script>
<style lang="scss" scoped>
.spot {
  width: 6px;
  height: 6px;
  display: inline-block;
  border-radius: 100%;
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}

.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.paginaCon.noborder {
  border-top: none;
  text-align: left;

  .pagination-container {
    padding: 0;
  }
}
</style>
<style lang="scss">
.selectd {
  td {
    background: #3f61f110;
  }
}
</style>
